package tx.bj12320.service;

import static junit.framework.Assert.assertNotNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

import tx.bj12320.model.TszbProcessInfo;
import tx.bj12320.model.UserInfo;
import tx.bj12320.model.enums.WorkOrderTypeEnum;
import tx.bj12320.vo.query.TszbProcessInfoQueryFacade;

import cn.org.rapid_framework.test.context.TestMethodContextExecutionListener;

@ContextConfiguration(locations = { "classpath:/spring/*-resource.xml",
		"classpath:/spring/*-validator.xml",
		"classpath:/spring/*-datasource.xml", "classpath:/spring/*-dao.xml",
		"classpath:/spring/*-service.xml" })
@TestExecutionListeners(listeners = TestMethodContextExecutionListener.class)
public class ModifyTszbDataTest extends
		AbstractTransactionalJUnit4SpringContextTests {
	protected Log log = LogFactory.getLog(getClass());

private TszbProcessInfoManager tszbProcessInfoManager;

	@Autowired
	public void setTszbProcessInfoManager(TszbProcessInfoManager manager) {
		this.tszbProcessInfoManager = manager;
	}
private ActivityInfoManager activityInfoManager;

	@Autowired
	public void setActivityInfoManager(ActivityInfoManager manager) {
		this.activityInfoManager = manager;
	}

	private UserInfoManager userInfoManager;
	@Autowired
	public void setUserInfoManager(UserInfoManager manager) {
		this.userInfoManager = manager;
	}
	protected DataSource getDataSource() {
		DataSource ds = (DataSource) applicationContext.getBean("dataSource");
		assertNotNull("not found 'dataSource'", ds);
		return ds;
	}


	/**
	 * 完善投诉转办流程：受理坐席 初审班长 审核人员 受理时间
	 */
	@Test
	@Rollback(false)
	public void modify_TszbProcessInfo_shouli_etc() {
		List<TszbProcessInfo> tis=tszbProcessInfoManager.findAll();
		for (TszbProcessInfo tszbProcessInfo : tis) {
			if(tszbProcessInfo.getCcXml()!=null){
				XStream x=new XStream(new StaxDriver());
				x.alias("TszbProcessInfoQuery", TszbProcessInfoQueryFacade.class);
				TszbProcessInfoQueryFacade qf=(TszbProcessInfoQueryFacade)x.fromXML(tszbProcessInfo.getCcXml());
				if(tszbProcessInfo.getWorkOrderType().equals(WorkOrderTypeEnum.TSWXCGZD.getCode())){
					tszbProcessInfo.setResolveWay(qf.getNoSheetReason());
				}

			}



			tszbProcessInfo.setShouliTime(tszbProcessInfo.getProcStartTime());

			String cuserid=tszbProcessInfo.getCcCreateuserid();
			UserInfo ui=userInfoManager.getByUserCode(cuserid);
			if(ui!=null){
				tszbProcessInfo.setShoulliPerson(ui.getRealName()+"("+ui.getAgentDn()+")");
			}



			try {
				Connection conn = getDataSource().getConnection();
				PreparedStatement ps = conn	.prepareStatement("select * from bj12320_activity_info where tszb_process_info_id="+tszbProcessInfo.getTszbProcessInfoId()+" and activity_name='bzcs'");
				ResultSet rs = ps.executeQuery();
				if(rs.next()){
					tszbProcessInfo.setChushenPerson(rs.getString("operator_name"));
				}
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				Connection conn = getDataSource().getConnection();
				PreparedStatement ps = conn	.prepareStatement("select * from bj12320_activity_info where tszb_process_info_id="+tszbProcessInfo.getTszbProcessInfoId()+" and activity_name='xtdbsh'");
				ResultSet rs = ps.executeQuery();
				if(rs.next()){
					tszbProcessInfo.setShenhePerson(rs.getString("operator_name"));
				}
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tszbProcessInfoManager.saveOrUpdate(tszbProcessInfo);

		}

	}

}
